package com.huang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huang.domain.LoginUser;
import com.huang.entity.User;
import com.huang.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author 黄自祥
 * @date 2022/05/03 14:33
 * @Description:
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws RuntimeException {
        //查询用户信息
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserName,username));
        if(ObjectUtils.isEmpty(user)){
            throw new RuntimeException("用户名或密码错误");
        }
        // 查询对应的权限信息
        List<String> authorities = Arrays.asList("test", "hello");
        LoginUser loginUser = new LoginUser(user, authorities);
        return loginUser;
    }
}
